package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.SystemAdminRoleManager;
import com.fish.anne.model.SystemAdminRole;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class SystemAdminRoleAction extends BaseAction implements Preparable {
    private SystemAdminRoleManager systemAdminRoleManager;
    private List systemAdminRoles;
    private SystemAdminRole systemAdminRole;
    private Long  arId;

    public void setSystemAdminRoleManager(SystemAdminRoleManager systemAdminRoleManager) {
        this.systemAdminRoleManager = systemAdminRoleManager;
    }

    public List getSystemAdminRoles() {
        return systemAdminRoles;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String systemAdminRoleId = getRequest().getParameter("systemAdminRole.arId");
            if (systemAdminRoleId != null && !systemAdminRoleId.equals("")) {
                systemAdminRole = systemAdminRoleManager.get(new Long(systemAdminRoleId));
            }
        }
    }

    public String list() {
        systemAdminRoles = systemAdminRoleManager.getAll();
        return SUCCESS;
    }

    public void setArId(Long  arId) {
        this. arId =  arId;
    }

    public SystemAdminRole getSystemAdminRole() {
        return systemAdminRole;
    }

    public void setSystemAdminRole(SystemAdminRole systemAdminRole) {
        this.systemAdminRole = systemAdminRole;
    }

    public String delete() {
        systemAdminRoleManager.remove(systemAdminRole.getArId());
        saveMessage(getText("systemAdminRole.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (arId != null) {
            systemAdminRole = systemAdminRoleManager.get(arId);
        } else {
            systemAdminRole = new SystemAdminRole();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (systemAdminRole.getArId() == null);

        systemAdminRoleManager.save(systemAdminRole);

        String key = (isNew) ? "systemAdminRole.added" : "systemAdminRole.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}